// 订单提交页面js
function getQueryString(key) {
    var reg = new RegExp("(^|&)" + key + "=([^&]*)(&|$)");
    var result = window.location.search.substr(1).match(reg);
    return result ? decodeURIComponent(result[2]) : null;
}

layui.use(['form', 'layedit', 'laydate'], function () {
    var form = layui.form
        , layer = layui.layer
        , layedit = layui.layedit
        , laydate = layui.laydate;
});
$(document).ready(function () {
    var userName = $("#username").val();
    var id = getQueryString("id");
    var buySum = getQueryString("buySum");
    $.ajax({
        url: "/order/getProById",
        type: "get",
        dataType: "json",
        data: {id: id, buySum: buySum},
        success: function (res) {
            if (res.code === 0) {
                var orderList = res.data;
                var str = "";
                var str1 = "";
                str += `
                    <dd class="item clearfix">
                    <div class="item-row">
                        <div class="col col-1">
                            <div class="g-pic">
                                <img src="` + orderList.productImg + `"                            
                                    width="50" height="50" />
                            </div>
                            <div class="g-info">
                                <a href="#">
                                    ${orderList.productName}
                                </a>
                            </div>
                        </div>

                        <div class="col col-2">${orderList.price}元</div>
                        <div class="col col-3">${orderList.buySum}</div>
                        <div class="col col-4">${orderList.sumPrice}<span>元</span></div>
                    </div>
                </dd>`;
                $("#orderList").append(str);
                var total_price = $("div.col.col-4").text();
                $("#total_price").html(total_price.substring(0, total_price.length - 1));
            }
        }
    });
    //下单
    $("#checkoutToPay").click(function () {
        layer.load(2);
        setTimeout(function () {
            layer.closeAll('loading');
        }, 1000);
        var proId = id;
        var buysum = buySum;
        var leave_message = $("#liuyan").val();
        var total_price = $("#total_price").text();
        var dataJson = {
            "user_id": userName, "product_id": proId, "buy_sum": buysum, "leave_message": leave_message,
            "total_price": total_price
        };
        console.log(dataJson);
        $.ajax({
            url: "/order/orderCommits",
            type: "post",
            dataType: "json",
            data: JSON.stringify(dataJson),
            contentType: "application/json;charset=utf-8",
            success: function (res) {
                if (res.code === 0) {
                    layer.load(2);
                    layer.alert("下单成功！", {
                        time: 1000,
                        icon: 1,
                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    });
                    setTimeout(function () {
                        window.location.href="/alipay/pay";
                    },2000);
                }
            }
        });
    });

    $("#J_editAddrOk").click(function () {
        //地址
        var consignee = $(".selected .itemConsignee").text();
        var tag = $(".selected .itemTag").text();
        var tel = $(".selected .itemTel").text();
        var region = $(".selected .itemRegion").text();
        var street = $(".selected .itemStreet").text();
        //支付方式
        var pay_way = $("#pay_way").text().trim();
        //配送方式 s
        var delivery_way = $("#delivery_way").text().trim();
        var dataAd = {
            "consignee": consignee, "tag": tag, "tel": tel, "region": region, "street": street,
            "pay_way": pay_way, "delivery_way": delivery_way, "userId": userName
        };
        $.ajax({
            url: "/address/checkAddress",
            type: "get",
            dataType: "json",
            data: {userid: userName},
            async: false,
            success: function (res) {
                if (res) {
                } else {
                    $.ajax({
                        url: "/order/saveAddress",
                        type: "post",
                        dataType: "json",
                        data: JSON.stringify(dataAd),
                        async: false,
                        contentType: "application/json;charset=utf-8",
                        success: function (res) {
                            if (res.code === 0) {
                                layer.alert("保存成功！", {
                                    time: 1000,
                                    icon: 1,
                                    skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                                });
                            }
                        }
                    });
                }
            }
        });
    });
    //地址修改
    $("#bianji").click(function () {
        //地址
        var consignee = $(".selected .itemConsignee").text();
        var tag = $(".selected .itemTag").text();
        var tel = $(".selected .itemTel").text();
        var region = $(".selected .itemRegion").text();
        var street = $(".selected .itemStreet").text();
        //支付方式
        var pay_way = $("#pay_way").text().trim();
        //配送方式d
        var delivery_way = $("#delivery_way").text().trim();
        var dataAdd = {
            "consignee": consignee, "tag": tag, "tel": tel, "region": region, "street": street,
            "pay_way": pay_way, "delivery_way": delivery_way, "userId": userName
        };
        $.ajax({
            url: "/order/updateAddress",
            type: "post",
            dataType: "json",
            data: JSON.stringify(dataAdd),
            success: function (res) {
                if (res.code === 0) {
                    layer.alert("修改成功！", {
                        icon: 2,
                        skin: 'layer-ext-moon' //该皮肤由layer.seaning.com友情扩展。关于皮肤的扩展规则，去这里查阅
                    });
                }
            }
        });
    });

});